package com.cloud.wms.ui.inside.transfer;

import android.os.Message;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.widget.ScanText;
import com.cloud.widget.expand.ExpandLayout;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.ui.inside.contract.TransferUpContact;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.inside.entity.TransferUpEntity;
import com.cloud.wms.ui.inside.presenter.TransferUpPresenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by FC on 2021/5/17.
 * Desc：移库上架扫描库位
 */
public class TransferUpStartActivity extends BaseBarListActivity<TransferUpEntity, TransferUpPresenter> implements TransferUpContact.View {
    @BindView(R.id.scan_text)
    ScanText scanText;

    @Override
    protected String barTitle() {
        return "移库上架";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_transfer_up_start;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_transfer_up_container;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        mPresenter.getCommodityMsg();
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.getCommodityMsg();
    }

    @Override
    protected void loadListData(int page) {
        super.loadListData(page);
        mPresenter.getCommodityMsg();
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            if (result.equalsIgnoreCase(mAdapter.getItem(i).getContainerCode())) {
                if (mAdapter.getItem(i).getCommodityList().size() == 1) {
                    TransferUpPosActivity.jumpToNext(this, mAdapter.getItem(i).getCommodityList().get(0));
                } else {
                    TransChooseBatchActivity.jumpToNext(this, mAdapter.getItem(i).getCommodityList(), false, null);
                }
                return;
            }
        }
        ArrayList<TransferCommodityEntity> commodityEntityList = new ArrayList<>();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            TransferUpEntity upEntity = mAdapter.getItem(i);
            for (int j = 0; j < upEntity.getCommodityList().size(); j++) {
                if (result.equalsIgnoreCase(upEntity.getCommodityList().get(j).getBarCode())) {
                    commodityEntityList.add(upEntity.getCommodityList().get(j));
                }
            }
        }
        if (commodityEntityList.size() == 1)
            TransferUpPosActivity.jumpToNext(this, commodityEntityList.get(0));
        else if (commodityEntityList.size() > 1)
            TransChooseBatchActivity.jumpToNext(this, commodityEntityList, false, null);
        else
            info("没有查询到符合条件的数据");
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, TransferUpEntity item) {
        helper.setText(R.id.tv_box_code, item.getContainerCode());
        ImageView ivArrow = helper.getView(R.id.iv_arrow);
        ExpandLayout expand = helper.getView(R.id.expand_layout);
        expand.setOnExpansionUpdateListener((expansionFraction, state) -> {
            ivArrow.setRotation(expansionFraction * 180);
        });
        helper.getView(R.id.ll_container).setOnClickListener(v -> expand.toggle(true));
        RecyclerView rvCommodity = helper.getView(R.id.rv_commodity);
        BaseQuickAdapter<TransferCommodityEntity, BaseViewHolder> mAdapter =
                new BaseQuickAdapter<TransferCommodityEntity, BaseViewHolder>(R.layout.item_transfer_up_commodity, item.getCommodityList()) {
                    @Override
                    protected void convert(@NonNull BaseViewHolder helper, TransferCommodityEntity commodityEntity) {
                        helper.setText(R.id.tv_bar_code, commodityEntity.getBarCode())
                                .setText(R.id.tv_commodity_name, commodityEntity.getCommodityName())
                                .setText(R.id.tv_production_date, commodityEntity.getProductionDate())
                                .setText(R.id.tv_expire_date, commodityEntity.getExpirationDate())
                                .setText(R.id.tv_in_date, commodityEntity.getInDate())
                                .setText(R.id.tv_stock_type, commodityEntity.getStockType() == 1 ? "良品" : "次品")
                                .setText(R.id.tv_amount, String.valueOf(commodityEntity.getAmount()));
                    }
                };
        rvCommodity.setAdapter(mAdapter);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
    }

    @Override
    protected boolean isRefresh() {
        return true;
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.CLOSE_REFRESH:
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
                mPresenter.getCommodityMsg();
                break;
        }
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
